package com.ifcar.app.api.dao.impl;

import com.ifcar.app.api.dao.PublishApi;
import com.ifcar.app.data.dao.PublishData;
import com.ifcar.app.data.dao.impl.PublishDataImpl;
import com.ifcar.app.module.PublishMsg;
import com.ifcar.app.module.Question;
import com.ifcar.app.util.EC;
import com.ifcar.app.util.Valid;
import com.mongodb.DBCursor;
import com.mongodb.WriteResult;
import com.url.ajax.json.JSONObject;

/**
 * 信息发布业务层实现类
 * @author nxlixa
 *
 */
public class PublishApiImpl implements PublishApi {
	
	/**
	 * 数据层接口
	 */
	private PublishData mDataDao;
	
	public PublishApiImpl(){
		mDataDao = new PublishDataImpl();
	}
	
	// 发布求客信息
	@Override
	public JSONObject publishMsgFindPeople(PublishMsg pubObj) throws Exception {
		
		JSONObject message = null;
		
		/**
		 * 缺少参数错误 帐号等
		 */
		String array[] = {pubObj.location, pubObj.account, pubObj.begin, pubObj.end, pubObj.pnumber, pubObj.stime, pubObj.isOnwer};
		message = Valid.paramterMissValid(array);
		if(null != message){
			return message;
		}
		
		message = new JSONObject();
		/**
		 * 电话号码格式合法性验证
		 */
		if(!Valid.validPhone(pubObj.account)){
			message.put(EC.ERROR, EC.MSG_PHONE_FORMAT_ERROR);
			message.put(EC.CODE, EC.CODE_PHONE_FORMAT_ERROR);
			return message;
		}
		
		WriteResult wr = mDataDao.publishMsgFindPeople(pubObj);
		
		if(null == wr.getError()){
			message.put(EC.SUCCESS, EC.SUCCESS);
		} else {
			message.put(EC.ERROR, EC.MSG_SERVER_ERROR);
			message.put(EC.CODE, EC.CODE_SERVER_ERROR);
		}
		
		return message;
		
	}

	// 发布求车信息
	@Override
	public JSONObject publishMsgFindCar(PublishMsg pubObj) throws Exception{
		
		JSONObject message = null;
		
		/**
		 * 缺少参数错误 帐号等
		 */
		String array[] = {pubObj.location, pubObj.account, pubObj.begin, pubObj.end, pubObj.pnumber, pubObj.stime, pubObj.isOnwer};
		message = Valid.paramterMissValid(array);
		if(null != message){
			return message;
		}
		
		message = new JSONObject();
		/**
		 * 电话号码格式合法性验证
		 */
		if(!Valid.validPhone(pubObj.account)){
			message.put(EC.ERROR, EC.MSG_PHONE_FORMAT_ERROR);
			message.put(EC.CODE, EC.CODE_PHONE_FORMAT_ERROR);
			return message;
		}
		
		WriteResult wr = mDataDao.publishMsgFindCar(pubObj);
		
		if(null == wr.getError()){
			message.put(EC.SUCCESS, EC.SUCCESS);
		} else {
			message.put(EC.ERROR, EC.MSG_SERVER_ERROR);
			message.put(EC.CODE, EC.CODE_SERVER_ERROR);
		}
		
		return message;
	}

	// 发布问题
	@Override
	public JSONObject publishQuestioin(Question qu) throws Exception {

		JSONObject message = null;
		
		/**
		 * 缺少参数错误 帐号等
		 */
		String array[] = {qu.account, qu.location, qu.questionContent};
		message = Valid.paramterMissValid(array);
		if(null != message){
			return message;
		}
		
		message = new JSONObject();
		/**
		 * 电话号码格式合法性验证
		 */
		if(!Valid.validPhone(qu.account)){
			message.put(EC.ERROR, EC.MSG_PHONE_FORMAT_ERROR);
			message.put(EC.CODE, EC.CODE_PHONE_FORMAT_ERROR);
			return message;
		}
		
		WriteResult wr = mDataDao.publishQuestioin(qu);
		
		if(null == wr.getError()){
			message.put(EC.SUCCESS, EC.SUCCESS);
		} else {
			message.put(EC.ERROR, EC.MSG_SERVER_ERROR);
			message.put(EC.CODE, EC.CODE_SERVER_ERROR);
		}
		
		return message;
	}

	// 回答问题
	@Override
	public JSONObject publishAnswer4Questioin(Question qu) throws Exception {
		
		JSONObject message = null;
		
		/**
		 * 缺少参数错误 帐号等
		 */
		String array[] = {qu.questionId, qu.account, qu.location, qu.questionContent};
		message = Valid.paramterMissValid(array);
		if(null != message){
			return message;
		}
		
		message = new JSONObject();
		/**
		 * 电话号码格式合法性验证
		 */
		if(!Valid.validPhone(qu.account)){
			message.put(EC.ERROR, EC.MSG_PHONE_FORMAT_ERROR);
			message.put(EC.CODE, EC.CODE_PHONE_FORMAT_ERROR);
			return message;
		}
		
		/**
		 * 是否存在对应的问题
		 */
		DBCursor dbCur = mDataDao.findQuestionObjByQid(qu.questionId);
		if(null == dbCur || dbCur.count() == 0){
			message.put(EC.ERROR, EC.MSG_QUESTIOIN_NOT_EXIST_ERROR);
			message.put(EC.CODE, EC.CODE_QUESTIOIN_NOT_EXIST_ERROR);
			dbCur.close();
			return message;
		}
		dbCur = null;
		
		WriteResult wr = mDataDao.publishAnswer4Questioin(qu);
		
		if(null == wr.getError()){
			message.put(EC.SUCCESS, EC.SUCCESS);
		} else {
			message.put(EC.ERROR, EC.MSG_SERVER_ERROR);
			message.put(EC.CODE, EC.CODE_SERVER_ERROR);
		}
		
		return message;
	}

}
